<?php

namespace addons\shopro\controller;

use addons\shopro\model\User;
use app\common\model\GoldLog;

/**
 * 定时任务
 */
class Task extends Base
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    //解冻黄金券
    public function thawGold(){
       $userList = User::where(array(['freeze_gold','>','0']))->select();
       foreach ($userList as $key=>$val){
            $fixed_gold = $val['fixed_gold'];
            if($val['freeze_gold']>0){
                if($fixed_gold>$val['freeze_gold']){
                    $fixed_gold = $val['freeze_gold'];
                }
                //解冻，加入黄金券记录
                $userdata = [
                    'freeze_gold' =>$val['freeze_gold'] - $fixed_gold,
                    'thaw_gold' => $val['thaw_gold'] + $fixed_gold,
                ];
                User::save(array($userdata),array('id'=>$val['id']));
                $goldData = [
                    'user_id'=>$val['id'],
                    'gold' =>$fixed_gold,
                    'memo' => '定时释放黄金券',
                    'create_time'=>time(),
                    'type'=>'thaw','create_at'=>date('Y-m-d')
                ];
                GoldLog::strict(false)->insert($goldData);
            }
       }
    }


}
